package com.csi.controller;

import cn.hutool.jwt.JWTUtil;
import com.csi.domain.FacilityInfo;
import com.csi.domain.RoomType;
import com.csi.service.RoomTypeService;
import com.csi.utils.JsonResult;
import com.csi.utils.ReturnResults;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * 房型管理
 */
@RequestMapping("/roomType")
@RestController
@Tag(name="RoomType",description = "酒店房型相关接口")
public class RoomTypeController {

    @Autowired
    private RoomTypeService roomTypeService;

    /**
     * 保存房型
     * @param roomType 房间类型
     * @return 请求结果
     */
    @RequestMapping(value = "/save")
    public Object save(RoomType roomType, @RequestParam("photo")MultipartFile[] multipartFiles,HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        roomType.setHotelId(hotelId);
        /*int save = roomTypeService.save(roomType);
        if (save == roomType.getRoomNum() + 1){
            return new Result("保存成功", ReturnResults.ACCESS_SUCCESS);
        }
        return new Result("保存失败", ReturnResults.ACCESS_FAILED);*/
        int saved =  roomTypeService.save(roomType,multipartFiles);
        if (saved > 0){
            return new JsonResult<>();
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"新增失败");
    }

    /**
     * 更新房型
     * @param roomType 房型对象
     * @return 请求结果
     */
    @RequestMapping(value = "/update")
    public Object update(RoomType roomType, @RequestParam("photo")MultipartFile[] multipartFiles,HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        roomType.setHotelId(hotelId);
        int updated = roomTypeService.update(roomType,multipartFiles);
        if (updated > 0){
            return new JsonResult<>();
        }
        return new JsonResult<>( ReturnResults.ACCESS_FAILED,"更新失败");
    }

    /**
     * 根据id查找房型
     * @param roomtypeId 房型id
     * @return 请求结果
     */
    @GetMapping(value = "/findById")
    public Object findById(@RequestParam("id") long roomtypeId){
        RoomType roomType = roomTypeService.findById(roomtypeId);
        if (roomType != null){
            return new JsonResult<>(roomType);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"查找失败");
    }

    /**
     * 查找酒店全部房型
     * @return
     */
    @GetMapping(value = "/findAll")
    public Object findAll(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        List<RoomType> roomTypes =  roomTypeService.findAll(hotelId);
        if (roomTypes != null){
            return new JsonResult<>(roomTypes);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"当前酒店没有房型信息");
    }

    /**
     * 查找酒店房型设施
     * @param request
     * @return
     */
    @RequestMapping("findFacility")
    public Object findFacility(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        List<FacilityInfo> facilityInfos = roomTypeService.findFacility(hotelId);
        if (facilityInfos != null){
            return new JsonResult<>(facilityInfos);
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"当前酒店没有房型信息");
    }

    /**
     * 线下入住
     * @param roomtypeId 房型id
     * @return 执行结果
     */
    @GetMapping(value = "checkInOffline")
    public Object checkInOffline(@RequestParam("roomtypeId") long roomtypeId){
        int updated = roomTypeService.checkInOffline(roomtypeId);
        if (updated >0){
            return new JsonResult<>();
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"修改失败");
    }

    @GetMapping("findRooms")
    public Object findRooms(HttpServletRequest request){
        String token = request.getHeader("token");
        long hotelId = Long.parseLong(JWTUtil.parseToken(token).getPayload("hotelId").toString());
        Map<String,Object> rooms = roomTypeService.findRooms(hotelId);
        if (rooms != null){
            return new JsonResult<>(rooms.values());
        }
        return new JsonResult<>(ReturnResults.ACCESS_FAILED,"房间查找失败");
    }

}
